﻿using CKS.Core.Framework;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZR.Model.Enum;
using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP20
{
    /*
     * 已停用 ，新的处理类 ：OP20SaveDataAction
     *OP20 盖板组装 - 保存数据 
     *  DcCoverrivetingassemble
     */
    public class DB3051_Work01Action : PLCAction
    {
        /// <summary>
        /// 已停用
        /// </summary>
        /// <param name="_value"></param>
        public DB3051_Work01Action(DecodeValue _value) : base(_value)
        {
        }
        public override void Execute()
        {
            var replyPos = Value.GetPosition(3050, 1);
            var result = Value.Value.ToInt();
            string batchNo = GetPartBatchNo((int)PartTypeEnum.定位柱小焊环);
            if (result == 1)
            {
                string CoverCode = Value.GetLinkData(1).ToString();

                if (CoverCode.IsEmpty())
                {
                   
                    AckWriteInt16(replyPos, 1);
                    EventBus.AddLog($"OP20加工结果-{CoverCode}-条码为空");
                    return;
                }

                DcCoverrivetingassemble dc = null;
                
               // dc = db.Queryable<DcCoverrivetingassemble>().Where(a => a.CoverCode == CoverCode).OrderByDescending(a=>a.AssembleTime).First();

                bool add = false;
                if(dc == null)
                {
                    dc = new DcCoverrivetingassemble();
                    add = true;
                }
                dc.SmallSolderRingBatchCode = batchNo;
                dc.CoverCode = CoverCode;
                dc.CoverBlueCode = Value.GetLinkData(2).ToString();
                dc.PillarBlueCode = Value.GetLinkData(3).ToString();
                dc.CCD_2D1 = Value.GetLinkData(4).ToInt();
                dc.CCD_2D2 = Value.GetLinkData(5).ToInt();
                dc.CCD_3D = Value.GetLinkData(6).ToInt();
                dc.HoleDiameter = Value.GetLinkData(7).ToDecimal();
                dc.SlotWidth = Value.GetLinkData(8).ToDecimal();

                dc.SlotLenUpper = Value.GetLinkData(21).ToDecimal();
                dc.SlotLenLower = Value.GetLinkData(22).ToDecimal();
                dc.SlotWidthUpper = Value.GetLinkData(23).ToDecimal();
                dc.SlotWidthLower = Value.GetLinkData(24).ToDecimal();
                dc.Flatness = Value.GetLinkData(25).ToDecimal();
                dc.HeightMax = Value.GetLinkData(26).ToDecimal();
                dc.HeightMin = Value.GetLinkData(27).ToDecimal();
                dc.Profile = Value.GetLinkData(28).ToDecimal();
                dc.PositionDegree = Value.GetLinkData(29).ToDecimal();

                dc.PostRivetingLocatingPinGapL1 = Value.GetLinkData(51).ToDecimal();
                dc.PostRivetingLocatingPinGapL2 = Value.GetLinkData(52).ToDecimal();
                dc.PostRivetingLocatingPinGapR1 = Value.GetLinkData(53).ToDecimal();
                dc.PostRivetingLocatingPinGapR2 = Value.GetLinkData(54).ToDecimal();
                dc.PostRivetingLocatingPinHeightL1 = Value.GetLinkData(55).ToDecimal();
                dc.PostRivetingLocatingPinHeightL2 = Value.GetLinkData(56).ToDecimal();
                dc.PostRivetingLocatingPinHeightR1 = Value.GetLinkData(57).ToDecimal();
                dc.PostRivetingLocatingPinHeightR2 = Value.GetLinkData(58).ToDecimal();
                dc.PostRivetingLocatingPinVerticalityL1 = Value.GetLinkData(59).ToDecimal();
                dc.PostRivetingLocatingPinVerticalityL2 = Value.GetLinkData(60).ToDecimal();
                dc.PostRivetingLocatingPinVerticalityR1 = Value.GetLinkData(61).ToDecimal();
                dc.PostRivetingLocatingPinVerticalityR2 = Value.GetLinkData(62).ToDecimal();

                dc.PressResult1 = Value.GetLinkData(71).ToInt();
                dc.PressResult2 = Value.GetLinkData(72).ToInt();

                dc.PressValue1 = Value.GetLinkData(73).ToDecimal();
                dc.PressDisplacement1 = Value.GetLinkData(74).ToDecimal();

                dc.PressValue2 = Value.GetLinkData(75).ToDecimal();
                dc.PressDisplacement2 = Value.GetLinkData(76).ToDecimal();
                dc.SmallSolderRingBlueCode = ""; //因焊环是人工上料 无法获得子篮编码
                
                dc.AssembleTime = DateTime.Now;

                dc.LPostRivGapResult = Value.GetLinkData(77).ToInt();
                dc.RPostRivGapResult = Value.GetLinkData(78).ToInt();
                dc.TotalResult = Value.GetLinkData(79).ToInt();
                dc.PressTotalResult = Value.GetLinkData(80).ToInt();
                dc.PPinExistence1 = Value.GetLinkData(81).ToInt();
                dc.PPinExistence2 = Value.GetLinkData(82).ToInt();
                dc.WRExistence1 = Value.GetLinkData(83).ToInt();
                dc.WRExistence2 = Value.GetLinkData(84).ToInt();





                dc.CoverResult = Value.GetLinkData(99).ToInt();

                if (add)
                {
                    CatchAction(() => {

                        db.Insertable(dc).ExecuteCommand();
                        UpdateBasketStock(PartTypeEnum.定位柱小焊环);
                    }, "OP20加工结果保存数据库");
                   
                }
                else
                {
                    db.Updateable(dc).UpdateColumns(a=>new {
                       // a.CoverBlueCode,
                        a.PillarBlueCode,
                        a.PostRivetingLocatingPinGapL1,
                        a.PostRivetingLocatingPinGapL2,
                        a.PostRivetingLocatingPinGapR1,
                         a.PostRivetingLocatingPinGapR2,
                        a.PostRivetingLocatingPinHeightL1,
                         a.PostRivetingLocatingPinHeightL2,
                         a.PostRivetingLocatingPinHeightR1,
                        a.PostRivetingLocatingPinHeightR2,
                        a.PostRivetingLocatingPinVerticalityL1,
                        a.PostRivetingLocatingPinVerticalityL2,
                        a.PostRivetingLocatingPinVerticalityR1,
                        a.PostRivetingLocatingPinVerticalityR2,
                        a.PressValue1,
                        a.PressValue2,
                        a.LPostRivGapResult,
                        a.RPostRivGapResult,
                        a.TotalResult,
                        a.AssembleTime

                    }).ExecuteCommand();
                }


                // db.Insertable(dc).ExecuteCommand();

                //保存数据
                //读取子篮二维码和检测结果
                //string barcode = Value.GetLinkData(1).ToString();
                //int flag = Value.GetLinkData(2).ToInt();

                //logger.LogInformation($"保存数据 子篮码{barcode} 检测结果{flag}");
                //查找保存完成点位
                //EventBus.PLCUpdate("OP20请求保存数据");

                AckWriteInt16(replyPos, 1);
                EventBus.AddLog($"OP20加工结果-{CoverCode}-{dc.PillarBlueCode}-保存操作");
            }
            else
            {

                AckWriteInt16(replyPos, 0);
                EventBus.AddLog("OP20加工结果-重置保存状态");
            }
        }
    }
}
